﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LitTools
{
    /// <summary>
    /// 状态机
    /// </summary>
    public class StateMechine
    {
        /// <summary>
        /// 当前状态
        /// </summary>
        public IState CurState { get; private set; }
        /// <summary>
        /// 是否正在运行
        /// </summary>
        public bool IsPlaying { get; private set; }
        /// <summary>
        /// 状态列表
        /// </summary>
        private List<IState> stateList = new();
        /// <summary>
        /// 转换器列表
        /// </summary>
        private List<Translater> translaterList = new();

        #region 增减操作
        /// <summary>
        /// 添加状态
        /// </summary>
        /// <param name="state"></param>
        public void AddState(IState state)
        {
            if (stateList.Contains(state))
            {
                Debug.LogError("不可以添加重复状态");
                return;
            }
            stateList.Add(state);
        }
        /// <summary>
        /// 移除状态
        /// </summary>
        /// <param name="state"></param>
        public void RemoveState(IState state)
        {
            if (!stateList.Contains(state))
            {
                Debug.LogError("状态不在状态机中");
                return;
            }
            stateList.Add(state);
        }
        /// <summary>
        /// 添加转化器
        /// </summary>
        /// <param name="translater"></param>
        public void AddTranslater(Translater translater)
        {
            if (translaterList.Contains(translater))
            {
                Debug.LogError("不可以重复添加重复转化器");
                return;
            }
            translaterList.Add(translater);
        }
        /// <summary>
        /// 移除转化器
        /// </summary>
        /// <param name="translater"></param>
        public void RemoveTranslater(Translater translater)
        {
            if (!translaterList.Contains(translater))
            {
                Debug.LogError("转化器不存在状态机中");
                return;
            }
            translaterList.Add(translater);
        }
        #endregion

        #region 生命周期
        /// <summary>
        /// 运行状态机
        /// </summary>
        /// <param name="state"></param>
        public void Play(IState state)
        {
            if (IsPlaying)
            {
                Debug.LogError("状态机已经在运行中");
                return;
            }
            CurState = state;
            IsPlaying = true;
        }
        /// <summary>
        /// 停止状态机
        /// </summary>
        public void Stop()
        {
            IsPlaying = false;
        }
        /// <summary>
        /// 更新状态机
        /// </summary>
        public void Update()
        {
            if (!IsPlaying) return;
            if (translaterList != null
                && translaterList.Count > 0)
            {
                for (int i = 0; i < translaterList.Count; i++)
                {
                    if (translaterList[i].FromState == CurState
                        && translaterList[i].ToState != CurState
                        && translaterList[i].TranslateCheck())
                    {
                        //转换器检测成功
                        TurnToState(translaterList[i].ToState);
                        return;
                    }
                }
            }
            CurState.OnUpdate(this);
        }
        /// <summary>
        /// 状态转换
        /// </summary>
        /// <param name="state"></param>
        private void TurnToState(IState state)
        {
            CurState.OnExit(this);
            CurState = state;
            CurState.OnEnter(this);
        }
        #endregion
    }
}